package com.lemon.tuvi2015.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lemon.tuvi2015.R;
import com.lemon.tuvi2015.object.XongdatObject;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

public class XongdatAdapter extends BaseAdapter {

	Context context;
	ArrayList<XongdatObject> listxongdat;

	public XongdatAdapter(Context context, ArrayList<XongdatObject> listxongdat) {
		this.context = context;
		this.listxongdat = listxongdat;
	}

	@Override
	public int getCount() {
		return listxongdat.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.xongdat_item, null);
			holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
			holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
			holder.textViewtitle = (TextView) convertView.findViewById(R.id.textViewTitle);
			holder.textViewrate = (TextView) convertView.findViewById(R.id.textViewRate);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final XongdatObject zodiacObject = listxongdat.get(position);
		Picasso.with(context).load(zodiacObject.getimage()).into(holder.imageView, new Callback() {
			@Override
			public void onSuccess() {
				holder.progressBar.setVisibility(View.GONE);
			}

			@Override
			public void onError() {
			}
		});
		holder.textViewtitle.setText(zodiacObject.getyear() + ": " + zodiacObject.gettitle());
		holder.textViewrate.setText(zodiacObject.getrate());
		return convertView;
	}

	private class ViewHolder {
		ImageView imageView;
		TextView textViewtitle, textViewrate;
		ProgressBar progressBar;
	}
}
